package jun.home.dailycost.web;

import java.beans.PropertyEditorSupport;

import jun.home.dailycost.domain.Category;
import jun.home.dailycost.service.CategoryService;

public class CategoryEditor extends PropertyEditorSupport {

	private CategoryService categoryService;

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

//	public String getAsText() {
//		Object obj = getValue();
//		if (obj != null && obj instanceof Category) {
//			Category category = (Category) obj;
//			return category.getCid().toString();
//		}
//		return "";
//	}

	public void setAsText(String text)
			throws java.lang.IllegalArgumentException {
		Category cat = null;
		if (!(text == null || text.equals(""))) {
			int cid = 0;
			try {
				cid = Integer.parseInt(text);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			cat = categoryService.selectById(cid);
		}
		setValue(cat);
	}

}
